package com.hxd.otc.module.convenience.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseMvpDialogFragment;
import com.hxd.otc.module.convenience.bean.OrderInfoBean;
import com.hxd.otc.module.convenience.contract.PayDialogContract;
import com.hxd.otc.module.convenience.presenter.PayDialogPresenter;

/**
 * Created by lzy on 2018/12/11 0011.
 * description ${支付对话框}
 */
public class PayDialogFragment extends BaseMvpDialogFragment<PayDialogPresenter> implements PayDialogContract.IView {

    private PayPanelFragment payPanelFragment;

    @Override
    protected PayDialogPresenter createPresenter() {
        return new PayDialogPresenter(this);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                Fragment fragment = getChildFragmentManager().findFragmentByTag(PaymentMethodFragment.class.getSimpleName());
                if (fragment != null) {
                    getChildFragmentManager().popBackStack();
                    return true;
                } else {
                    return false;
                }
            }
        });
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_pay;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        replaceChildFragment(R.id.fl_container_pay, payPanelFragment, PayPanelFragment.class.getSimpleName());
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
    }

    public static PayDialogFragment getInstance() {
        return new PayDialogFragment();
    }

    @Override
    public void addChildFragment(int containerViewId, Fragment fragment, String tag) {
        getChildFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .setCustomAnimations(
                        R.anim.slide_right_in,
                        R.anim.slide_left_out,
                        R.anim.slide_left_in,
                        R.anim.slide_right_out
                )
                .add(containerViewId, fragment, tag)
                .commit();
    }

    public PayDialogFragment setData(OrderInfoBean bean) {
        payPanelFragment = PayPanelFragment.getInstance(bean);
        return this;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (!isAdded()) {
            return;
        }
        Fragment fragment = getChildFragmentManager().findFragmentByTag(PaymentMethodFragment.class.getSimpleName());
        if (fragment != null) {
            getChildFragmentManager()
                    .beginTransaction().remove(fragment).setCustomAnimations(
                    R.anim.slide_right_in,
                    0,
                    R.anim.slide_left_in,
                    0
            ).commit();
        }
    }
}
